#include "zerodialog.h"
#include "models/command.h"
#include "models/robot.h"
#include "ui_zerodialog.h"
#include "models/robotlist.h"

ZeroDialog::ZeroDialog(QWidget *parent)
    : PopupDialog(parent)
    , ui(new Ui::ZeroDialog)
{
    ui->setupUi(this);
    scaleWidgets(this,g_xScale,g_yScale);
    setupUI();
    Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
    if(pCurRobot)
        connect(pCurRobot,&Robot::sportStateChanged, this, &ZeroDialog::updateData,Qt::UniqueConnection);
}

ZeroDialog::~ZeroDialog()
{
    delete ui;
}


void ZeroDialog::scaleWidgets(QWidget *widget, double xScale, double yScale)
{
    setFixedSize(int(widget->width() * xScale),int(widget->height() * yScale));
    for (QWidget * child : widget->findChildren<QWidget*>()) {
        QRect geom = child->geometry();
        int width = qMax(1, static_cast<int>(int(geom.width() * xScale)));
        //当按钮高度比较大时使用y方向比例，当按钮比较小时使用x比例，防止按钮被压扁
        int height = qMax(1, static_cast<int>((geom.height() == geom.width() ? int(geom.height() * xScale) : int(geom.height() * yScale))));
        child->setFixedSize(width,height);
        child->move(int(geom.left() * xScale),int(geom.top() * yScale));
        QFont font = child->font();
        if(font.pixelSize()> 0)
            font.setPixelSize(int(font.pixelSize()*xScale));
        else if(font.pointSize()> 0)
            font.setPixelSize(int(font.pointSize()*xScale));
        child->setFont(font);
    }
}

void ZeroDialog::updateSelectAll()
{
    bool bChecked = true;
    do{
        if(!ui->checkBox_head->isChecked())
        {
            bChecked = false;
            break;
        }
        if(!ui->checkBox_waist->isChecked())
        {
            bChecked = false;
            break;
        }
        if(!ui->checkBox_leftarm->isChecked())
        {
            bChecked = false;
            break;
        }
        if(!ui->checkBox_rightarm->isChecked())
        {
            bChecked = false;
            break;
        }
        if(!ui->checkBox_leftleg->isChecked())
        {
            bChecked = false;
            break;
        }
        if(!ui->checkBox_rightleg->isChecked())
        {
            bChecked = false;
            break;
        }
    }while(0);
    ui->checkBox_all->setChecked(bChecked);
}

void ZeroDialog::on_pushButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}


void ZeroDialog::on_pushButton_3_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}


void ZeroDialog::on_pushButton_clicked()
{
    reject();
}

void ZeroDialog::on_checkBox_head_toggled(bool checked)
{
    Q_UNUSED(checked)
    updateSelectAll();
}

void ZeroDialog::on_checkBox_waist_toggled(bool checked)
{
    Q_UNUSED(checked)
    updateSelectAll();
}

void ZeroDialog::on_checkBox_leftarm_toggled(bool checked)
{
    Q_UNUSED(checked)
    updateSelectAll();
}

void ZeroDialog::on_checkBox_rightarm_toggled(bool checked)
{
    Q_UNUSED(checked)
    updateSelectAll();
}

void ZeroDialog::on_checkBox_leftleg_toggled(bool checked)
{
    Q_UNUSED(checked)
    updateSelectAll();
}

void ZeroDialog::on_checkBox_rightleg_toggled(bool checked)
{
    Q_UNUSED(checked)
    updateSelectAll();
}

void ZeroDialog::setupUI()
{
    QString strCheckBoxStyle = QString(
                                   "QCheckBox::indicator:unchecked{"
                                   "width: %1px;"
                                   "height: %1px; "
                                   "margin-top:%2px;"
                                   "    border-image:url(:/images/uncheck.png);"
                                   "}"
                                   "QCheckBox::indicator:checked{"
                                   "width: %1px;"
                                   "height: %1px; "
                                   "margin-top:%2px;"
                                   "    border-image:url(:/images/check.png);"
                                   "}"
                                   "QCheckBox{"
                                   "    color: white;"
                                   "    font: bold %3px \"微软雅黑\" ; "
                                   "}").arg(int(20*g_xScale)).arg(int(0*g_xScale)).arg(int(18*g_xScale));
    ui->checkBox_head->setStyleSheet(strCheckBoxStyle);
    ui->checkBox_waist->setStyleSheet(strCheckBoxStyle);
    ui->checkBox_leftarm->setStyleSheet(strCheckBoxStyle);
    ui->checkBox_rightarm->setStyleSheet(strCheckBoxStyle);
    ui->checkBox_leftleg->setStyleSheet(strCheckBoxStyle);
    ui->checkBox_rightleg->setStyleSheet(strCheckBoxStyle);
    ui->checkBox_all->setStyleSheet(strCheckBoxStyle);
    createJointsByType(HEAD_JOINT,ui->checkBox_head);
    createJointsByType(WAIST_JOINT,ui->checkBox_waist);
    createJointsByType(LEFT_ARM_JOINT,ui->checkBox_leftarm);
    createJointsByType(RIGHT_ARM_JOINT,ui->checkBox_rightarm);
    createJointsByType(LEFT_LEG_JOINT,ui->checkBox_leftleg);
    createJointsByType(RIGHT_LEG_JOINT,ui->checkBox_rightleg);
}


void ZeroDialog::createJointsByType(JointType type, QCheckBox *pCheckBox)
{
    QVector<JOINT>joints = getJointByType(type);
    QRect rc = pCheckBox->geometry();
    QPoint pt = rc.topLeft();
    QString strCheckBoxStyle = QString(
                                   "QCheckBox::indicator:unchecked{"
                                   "width: %1px;"
                                   "height: %1px; "
                                   "margin-top:%2px;"
                                   "    border-image:url(:/images/uncheck.png);"
                                   "}"
                                   "QCheckBox::indicator:checked{"
                                   "width: %1px;"
                                   "height: %1px; "
                                   "margin-top:%2px;"
                                   "    border-image:url(:/images/check.png);"
                                   "}"
                                   "QCheckBox{"
                                   "    color: white;"
                                   "    font: %3px \"微软雅黑\";"
                                   "}").arg(int(20*g_xScale)).arg(int(0*g_xScale)).arg(int(18*g_xScale));
    QString strLabelStyle = QString("QLabel{"
                                    "    color: lightgray;"
                                    "    font: %1px \"微软雅黑\";"
                                    "}").arg(int(18*g_xScale));

    pt.setY(pt.y() + 15 * g_yScale);
    for(int i = 0; i < joints.size(); ++i)
    {
        pt.setY(pt.y() + 35 * g_yScale);
        QCheckBox * pJointBox = new QCheckBox(ui->page_2);
        pJointBox->setProperty("type",type);
        pJointBox->setText(joints[i].strName);
        pJointBox->move(pt);
        pJointBox->setStyleSheet(strCheckBoxStyle);
        QLabel * pJointLab = new QLabel(ui->page_2);
        pJointLab->move(pt.x() + 200*g_xScale,pt.y());
        pJointLab->setText("0.00");
        pJointLab->setStyleSheet(strLabelStyle);
        m_mpCbx.insert(joints[i].nIndex,pJointBox);
        m_mpLabel.insert(joints[i].nIndex,pJointLab);
        connect(pJointBox,&QCheckBox::clicked, this, &ZeroDialog::onJointCbxClicked);
    }
}


void ZeroDialog::on_checkBox_all_clicked()
{
    bool bCheck = ui->checkBox_all->isChecked();
    ui->checkBox_head->setChecked(bCheck);
    ui->checkBox_waist->setChecked(bCheck);
    ui->checkBox_leftarm->setChecked(bCheck);
    ui->checkBox_rightarm->setChecked(bCheck);
    ui->checkBox_leftleg->setChecked(bCheck);
    ui->checkBox_rightleg->setChecked(bCheck);
    for(auto it = m_mpCbx.begin(); it != m_mpCbx.end(); ++it)
    {
        if(it.value())
            it.value()->setChecked(bCheck);
    }
}


void ZeroDialog::on_checkBox_head_clicked()
{
    QVector<JOINT>joints = getJointByType(HEAD_JOINT);
    for(int i = 0; i < joints.size(); ++i)
    {
        m_mpCbx[joints[i].nIndex]->setChecked(ui->checkBox_head->isChecked());
    }
}


void ZeroDialog::on_checkBox_waist_clicked()
{
    QVector<JOINT>joints = getJointByType(WAIST_JOINT);
    for(int i = 0; i < joints.size(); ++i)
    {
        m_mpCbx[joints[i].nIndex]->setChecked(ui->checkBox_waist->isChecked());
    }
}


void ZeroDialog::on_checkBox_leftarm_clicked()
{
    QVector<JOINT>joints = getJointByType(LEFT_ARM_JOINT);
    for(int i = 0; i < joints.size(); ++i)
    {
        m_mpCbx[joints[i].nIndex]->setChecked(ui->checkBox_leftarm->isChecked());
    }
}


void ZeroDialog::on_checkBox_rightarm_clicked()
{
    QVector<JOINT>joints = getJointByType(RIGHT_ARM_JOINT);
    for(int i = 0; i < joints.size(); ++i)
    {
        m_mpCbx[joints[i].nIndex]->setChecked(ui->checkBox_rightarm->isChecked());
    }
}


void ZeroDialog::on_checkBox_leftleg_clicked()
{
    QVector<JOINT>joints = getJointByType(LEFT_LEG_JOINT);
    for(int i = 0; i < joints.size(); ++i)
    {
        m_mpCbx[joints[i].nIndex]->setChecked(ui->checkBox_leftleg->isChecked());
    }
}


void ZeroDialog::on_checkBox_rightleg_clicked()
{
    QVector<JOINT>joints = getJointByType(RIGHT_LEG_JOINT);
    for(int i = 0; i < joints.size(); ++i)
    {
        m_mpCbx[joints[i].nIndex]->setChecked(ui->checkBox_rightleg->isChecked());
    }
}

void ZeroDialog::onJointCbxClicked()
{
    QCheckBox *pCheckBox=qobject_cast<QCheckBox*>(sender());
    int type = pCheckBox->property("type").toInt();
    QVector<JOINT>joints = getJointByType((JointType)type);
    bool bCheckAll = true;
    for(int i = 0; i < joints.size(); ++i)
    {
        QCheckBox * p = m_mpCbx[joints[i].nIndex];
        if(p && !p->isChecked())
        {
            bCheckAll = false;
            break;
        }
    }
    switch (type) {
    case HEAD_JOINT:
        ui->checkBox_head->setChecked(bCheckAll);
        break;
    case WAIST_JOINT:
        ui->checkBox_waist->setChecked(bCheckAll);
        break;
    case LEFT_ARM_JOINT:
        ui->checkBox_leftarm->setChecked(bCheckAll);
        break;
    case RIGHT_ARM_JOINT:
        ui->checkBox_rightarm->setChecked(bCheckAll);
        break;
    case LEFT_LEG_JOINT:
        ui->checkBox_leftleg->setChecked(bCheckAll);
        break;
    case RIGHT_LEG_JOINT:
        ui->checkBox_rightleg->setChecked(bCheckAll);
        break;
    default:
        break;
    }
}

void ZeroDialog::on_pushButton_4_clicked()
{
    QVector<int> vtJointIndex;
    for(auto it = m_mpCbx.begin(); it != m_mpCbx.end(); ++it)
    {
        if(it.value()->isChecked())
        {
            vtJointIndex.push_back(it.key());
        }
    }
    Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
    if(pCurRobot)
    {
        pCurRobot->zeroJoints(vtJointIndex);
    }
}

void ZeroDialog::updateData()
{
    Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
    if(pCurRobot)
    {
        SportState SportState=pCurRobot->SportStateData();
        for(auto it = m_mpLabel.begin(); it != m_mpLabel.end(); ++it)
        {
            if(it.key() < 31)
            {
                it.value()->setText(QString::number(SportState.realArc[it.key()], 'f', 2) );
            }
        }
    }
}

